package com.marvels.product.productmanage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.CommonProperties;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.MarvelsUtils;
import com.marvels.common.utils.ServerProperties;
import com.marvels.common.utils.StringUtils;
import com.marvels.product.common.api.ProductUmConsumerApi;
import com.marvels.product.param.dao.McptAmortMethodMapper;
import com.marvels.product.param.dao.McptConsultingFeeMapper;
import com.marvels.product.param.dao.McptCustomerTypeParamDao;
import com.marvels.product.param.dao.McptPaymentSequenceMapper;
import com.marvels.product.param.dao.McptProductMapper;
import com.marvels.product.param.dto.McptAmortMethodDTO;
import com.marvels.product.param.dto.McptConsultingFeeDTO;
import com.marvels.product.param.dto.McptCustomerTypeParamDTO;
import com.marvels.product.param.dto.McptPaymentSequenceDTO;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.productmanage.dao.McptChannelDao;
import com.marvels.product.productmanage.dao.ProductTypeDao;
import com.marvels.product.productmanage.dto.McptChannelDto;
import com.marvels.product.productmanage.dto.McptProductDto;
import com.marvels.product.productmanage.dto.McptProductFundRelationDto;
import com.marvels.product.productmanage.dto.McptProductMappedDto;
import com.marvels.product.productmanage.dto.McptProductTypeChannelDto;
import com.marvels.product.productmanage.dto.McptProductTypeDto;
import com.marvels.product.productmanage.dto.McptProductTypeTermDto;
import com.marvels.product.productmanage.dto.McptProductVersionDto;
import com.marvels.product.productmanage.dto.OrganizationInfoDTO;
import com.marvels.product.productmanage.dto.ProductRequestDTO;
import com.marvels.product.productmanage.service.ProductTypeService;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {
	@Autowired
	ProductTypeDao productTypeDao;
	@Autowired
	McptChannelDao mcptChannelDao;
	@Autowired
	private McptAmortMethodMapper mcptAmortMethodMapper;
	@Autowired
	private McptPaymentSequenceMapper mcptPaymentSequenceMapper;
	@Autowired
	private McptCustomerTypeParamDao McptCustomerTypeParamDao;
	@Autowired
	private McptConsultingFeeMapper mcptConsultingFeeMapper;
	@Autowired
	private McptProductMapper mcptProductMapper;
	@Autowired
	private ProductUmConsumerApi umConsumerApi;
	@Value("${config.properties.path}")
	private  String configPropertiesPath;
	@Override
	public List<McptProductTypeDto> queryProduct(McptProductTypeDto mcptProductTypeDto) throws MarvelsBusinessException {
		if (mcptProductTypeDto != null) {
			if (mcptProductTypeDto.getCurrentPage() != null && mcptProductTypeDto.getPageSize() != null) {
				PageHelper.startPage(mcptProductTypeDto.getCurrentPage(), mcptProductTypeDto.getPageSize());
			}
		}
		return productTypeDao.selectProduct(mcptProductTypeDto);

	}

	public List<Map<String, Object>> queryVersion(McptProductVersionDto mcptProductVersionDto)
			throws MarvelsBusinessException {
		return productTypeDao.selectVersion(mcptProductVersionDto);
	}

	@Override
	public Integer queryCountProduct(Map<String, Object> map) throws MarvelsBusinessException {
			return productTypeDao.selectCountProduct(map);
	}

	@Override
	@Transactional
	public void productUpdate(McptProductVersionDto mcptProductVersionDto) throws MarvelsBusinessException {
		Integer validNum = productTypeDao.checkUnique(mcptProductVersionDto);
		String mark = null;
		if (mcptProductVersionDto != null) {
			mark = mcptProductVersionDto.getMark();
		} else {
			throw new MarvelsBusinessException("传入的版本参数为空");
		}
		if ("start".equals(mark) && validNum > 0) {
			throw new MarvelsBusinessException("已经有一个版本在使用中");
		} else if ("start".equals(mark) && validNum < 1) {
			productTypeDao.updateProductVersion(mcptProductVersionDto);
			productTypeDao.updateSProduct(mcptProductVersionDto);
		}
		if ("stop".equals(mark)) {
			productTypeDao.updateProductVersion(mcptProductVersionDto);
			productTypeDao.updateSProduct(mcptProductVersionDto);
		}
	}

	@Override
	@Transactional
	public void productDelete(McptProductVersionDto mcptProductVersionDto) throws MarvelsBusinessException {
		productTypeDao.updateProductVersion(mcptProductVersionDto);
		productTypeDao.updateSProduct(mcptProductVersionDto);
	}

	@Override
	@Transactional
	public void productInsert(McptProductTypeDto mcptProductTypeDto) throws MarvelsBusinessException {
		if (mcptProductTypeDto == null) {
			throw new MarvelsBusinessException("产品大类不能为空");
		}
		if (StringUtil.isEmpty(mcptProductTypeDto.getTitleCase())) {
			throw new MarvelsBusinessException("产品首字母大写名称不能为空");
		}
		if (StringUtil.isEmpty(mcptProductTypeDto.getProductTypeName())) {
			throw new MarvelsBusinessException("产品名称不能为空");
		}
		UserDTO user = CacheUtils.getCacheUser();
		String userName = "";
		if(user != null){
			userName = user.getUserName();
		}
		String titleCase = mcptProductTypeDto.getTitleCase().toUpperCase();
		String productTypeName = mcptProductTypeDto.getProductTypeName().replace(" ", "");
		mcptProductTypeDto.setProductType(MarvelsUtils.generateUUIDCode("PT"));
		mcptProductTypeDto.setTitleCase(titleCase);
		mcptProductTypeDto.setProductTypeName(productTypeName);
		mcptProductTypeDto.setUserName(userName);
		List<McptProductTypeDto> AllMc = productTypeDao.selectProduct(null);
		for (McptProductTypeDto mc : AllMc) {
			if (productTypeName.trim().equals(mc.getProductTypeName()) || titleCase.trim().equals(mc.getTitleCase())) {// 验证产品名称是否存在
				throw new MarvelsBusinessException("产品名称或简称已经存在");
			}
		}
		productTypeDao.insertProduct(mcptProductTypeDto);
		// 插入期数表
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<String> termList = mcptProductTypeDto.getTermList();
		MarvelsLogUtil.getInstance().info("the term list is====>" + termList);
		String productType = mcptProductTypeDto.getProductType();
		MarvelsLogUtil.getInstance().info("the product code is ===>" + productType);
		for (String term : termList) {
			Map<String, Object> termMap = new HashMap<String, Object>();
			termMap.put("productType", productType);
			termMap.put("term", term);
			termMap.put("productTypeName", productTypeName);
			termMap.put("userName", userName);
			list.add(termMap);
		}
		productTypeDao.insertTerm(list);
		// 插入产品对应渠道信息表
		List<String> channelarr = mcptProductTypeDto.getChannelList();
		List<Map<String, Object>> channelList = new ArrayList<Map<String, Object>>();

		for (String channelCode : channelarr) {
			Map<String, Object> termMap = new HashMap<String, Object>();
			termMap.put("productType", productType);
			termMap.put("channelCode", channelCode);
			termMap.put("productTypeName", productTypeName);
			termMap.put("userName", userName);
			channelList.add(termMap);
		}
		productTypeDao.insertChannel(channelList);
	}

	@Override
	public List<McptChannelDto> queryChannel(McptProductTypeDto mcptProductTypeDto) throws MarvelsBusinessException {
		return mcptChannelDao.selectChannel(mcptProductTypeDto);
	}

	@Override
	public List<McptProductTypeChannelDto> queryChannelByProductCode(McptProductTypeDto mcptProductTypeDto)
			throws MarvelsBusinessException {
		return productTypeDao.selectChannelByProductCode(mcptProductTypeDto);
	}

	@Override
	public List<McptProductTypeTermDto> queryTermByProductCode(McptProductTypeDto mcptProductTypeDto)
			throws MarvelsBusinessException {
		return productTypeDao.selectTermByProductCode(mcptProductTypeDto);
	}

	@Override
	@Transactional
	public void editConfirm(McptProductTypeDto mcptProductTypeDto) throws MarvelsBusinessException {
		if (mcptProductTypeDto == null) {
			throw new MarvelsBusinessException("产品大类参数不能为空");
		}
		UserDTO user = CacheUtils.getCacheUser();
		String userName = "";
		if(user != null){
			userName = user.getUserName();
		}
		productTypeDao.updateById(mcptProductTypeDto);
		productTypeDao.updateChannelByProductType(mcptProductTypeDto);// 逻辑删除老的渠道和期限
		productTypeDao.updateTermByProductType(mcptProductTypeDto);// 逻辑删除老的渠道和期限
		// 插入期数表
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<String> termArr = mcptProductTypeDto.getTermList();
		MarvelsLogUtil.getInstance().info("the term list is====>" + termArr);
		String productType = mcptProductTypeDto.getProductType();
		MarvelsLogUtil.getInstance().info("the product code is ===>" + productType);
		for (String term : termArr) {
			Map<String, Object> termMap = new HashMap<String, Object>();
			termMap.put("productType", productType);
			termMap.put("term", term);
			termMap.put("productTypeName", mcptProductTypeDto.getProductTypeName());
			termMap.put("userName", userName);
			list.add(termMap);
		}
		productTypeDao.insertTerm(list);
		// 插入产品对应渠道信息表
		List<Map<String, Object>> channelList = new ArrayList<Map<String, Object>>();
		List<String> channelArr = mcptProductTypeDto.getChannelList();
		for (String channelCode : channelArr) {
			Map<String, Object> termMap = new HashMap<String, Object>();
			termMap.put("productType", productType);
			termMap.put("channelCode", channelCode);
			termMap.put("productTypeName", mcptProductTypeDto.getProductTypeName());
			termMap.put("userName", userName);
			channelList.add(termMap);
		}
		productTypeDao.insertChannel(channelList);
	}

	@Override
	public void generateVersion(McptProductTypeDto mcptProductTypeDto) throws MarvelsBusinessException {
		if (mcptProductTypeDto == null) {
			throw new MarvelsBusinessException("产品大类参数不能为空");
		}
		Integer num = productTypeDao.selectByVersion(mcptProductTypeDto);
		if (num > 0) {
			throw new MarvelsBusinessException("版本号已经存在");
		}
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<String> channelList = mcptProductTypeDto.getChannelList();
		List<String> termList = mcptProductTypeDto.getTermList();
		List<String> gradeList = mcptProductTypeDto.getGradeList();
		String productTypeName = mcptProductTypeDto.getProductTypeName();
		String productVersion = mcptProductTypeDto.getProductVersion();
		String productType = mcptProductTypeDto.getProductType();
		String desc = mcptProductTypeDto.getDesc();
		String cycle =mcptProductTypeDto.getBillingCycle();
		String amortMethod = mcptProductTypeDto.getAmortMethod();
		Integer graceDays = mcptProductTypeDto.getGraceDays();
		String repaySeq = mcptProductTypeDto.getRepaySeq();
		UserDTO user = CacheUtils.getCacheUser();
		String userName = "";
		if(user != null){
			userName = user.getUserName();
		}
		for (String channel : channelList) {
			for (String term : termList) {
				for (String grade : gradeList) {
					Map<String, Object> tmpMap = new HashMap<String, Object>();
					tmpMap.put("productType", productType);
					tmpMap.put("productVersion", productVersion);
					tmpMap.put("productName", productTypeName + "-" + channel + "-" + term + "-" + grade);
					tmpMap.put("productCode", MarvelsUtils.generateUUIDCode("PT"));
					tmpMap.put("status", 9);
					tmpMap.put("userName", userName);
					tmpMap.put("productTypeName", productTypeName);
					tmpMap.put("desc", desc);
					tmpMap.put("cycle", cycle);
					tmpMap.put("amortMethod", amortMethod);
					tmpMap.put("graceDays", graceDays);
					tmpMap.put("tenor",term);
					tmpMap.put("repaySeq", repaySeq);
					tmpMap.put("grade", grade);
					list.add(tmpMap);
				}
			}
		}
		productTypeDao.insertSProduct(list);
		productTypeDao.insertMcptProductVersion(mcptProductTypeDto);
	}

	@Override
	public List<McptProductTypeDto> queryProductVersion(McptProductTypeDto mcptProductTypeDto)
			throws MarvelsBusinessException {
		if (mcptProductTypeDto != null) {
			if (mcptProductTypeDto.getCurrentPage() != null && mcptProductTypeDto.getPageSize() != null) {
				PageHelper.startPage(mcptProductTypeDto.getCurrentPage(), mcptProductTypeDto.getPageSize());
			}
		}
		return productTypeDao.selectProductVersion(mcptProductTypeDto);
	}

	@Override
	public List<McptProduct> queryByVersion(McptProductTypeDto mcptProductTypeDto) throws MarvelsBusinessException {
		if (mcptProductTypeDto != null) {
			if (mcptProductTypeDto.getCurrentPage() != null && mcptProductTypeDto.getPageSize() != null) {
				PageHelper.startPage(mcptProductTypeDto.getCurrentPage(), mcptProductTypeDto.getPageSize());
			}
		}
		return productTypeDao.selectSProductByVersion(mcptProductTypeDto);
	}

	@Override
	@Transactional
	public void saveProductFund(McptProductFundRelationDto mcptProductFundRelationDto) throws MarvelsBusinessException {
		if (mcptProductFundRelationDto == null) {
			throw new MarvelsBusinessException("产品映射资金源参数不能为空");
		}
		System.out.println(mcptProductFundRelationDto);
		List<McptProductFundRelationDto> list = new ArrayList<McptProductFundRelationDto>();
		List<Map<String,String>> productValueList = mcptProductFundRelationDto.getProductValueList();
		for (Map<String,String> item : productValueList) {
			McptProductFundRelationDto tmp = new McptProductFundRelationDto();
			tmp.setProductVersion(mcptProductFundRelationDto.getProductVersion());
			tmp.setProductType(mcptProductFundRelationDto.getProductType());
			tmp.setProductTypeName(mcptProductFundRelationDto.getProductTypeName());
			tmp.setProductCode(item.get("productCode"));
			tmp.setProductName(item.get("productName"));
			tmp.setFundCode(mcptProductFundRelationDto.getFundCode());
			tmp.setFundName(mcptProductFundRelationDto.getFundName());
			tmp.setLevel(mcptProductFundRelationDto.getLevel());
			tmp.setRate(mcptProductFundRelationDto.getRate());
			tmp.setStatus(mcptProductFundRelationDto.getStatus());
			list.add(tmp);
		}
		List<McptProductFundRelationDto> resultList = productTypeDao.selectMappingOfProductFundByProductCode(list);
		if (resultList.size() > 0) {
			productTypeDao.updateProdutMappingFundByIds(resultList);
		}
		productTypeDao.insertMappingOfProductFund(list);
	}

	@Override
	public List<McptProductFundRelationDto> queryProductMappingFund(
			McptProductFundRelationDto mcptProductFundRelationDto) throws MarvelsBusinessException {
		if (mcptProductFundRelationDto != null) {
			if (mcptProductFundRelationDto.getCurrentPage() != null
					&& mcptProductFundRelationDto.getPageSize() != null) {
				PageHelper.startPage(mcptProductFundRelationDto.getCurrentPage(),
						mcptProductFundRelationDto.getPageSize());
			}
		}
		return productTypeDao.selectProdutMappingFund(mcptProductFundRelationDto);
	}

	@Override
	public List<McptProductFundRelationDto> removeProductMappingFundById(McptProductFundRelationDto mcptProductFundRelationDto)
			throws MarvelsBusinessException {
		productTypeDao.deleteProdutMappingFundById(mcptProductFundRelationDto);
		return this.queryProductMappingFund(mcptProductFundRelationDto);
	}

	@Override
	public void updateProductMappingFundById(McptProductFundRelationDto mcptProductFundRelationDto)
			throws MarvelsBusinessException {
		mcptProductFundRelationDto.setUpdatedBy(CacheUtils.getCacheUser().getUserName());
		productTypeDao.deleteProdutMappingFundById(mcptProductFundRelationDto);
	}

	@Override
	public void addproductMappedChannel(McptProductMappedDto mcptProductMappedDto)
			throws MarvelsBusinessException {
		if (mcptProductMappedDto == null) {
			throw new MarvelsBusinessException("产品映射渠道参数不能为空");
		}
		List<Map<String,String>>  productValueList = mcptProductMappedDto.getProductValueList();
		List<McptProductMappedDto> ProductMappedList = new ArrayList<McptProductMappedDto>();
		//传入的productValueList是产品小类 code+":"+name形式的集合,获取批量的产品映射渠道关系信息
		for(Map<String,String> product:productValueList){
			McptProductMappedDto tmp = new McptProductMappedDto();
			tmp.setChannel(mcptProductMappedDto.getChannel());
			tmp.setChannelName(mcptProductMappedDto.getChannelName());
			tmp.setCompositeMonthlyRate(mcptProductMappedDto.getCompositeMonthlyRate());
			tmp.setCreatedBy(mcptProductMappedDto.getCreatedBy());
			tmp.setProductTypeName(mcptProductMappedDto.getProductTypeName());
			tmp.setProductType(mcptProductMappedDto.getProductType());
			tmp.setProductVersion(mcptProductMappedDto.getProductVersion());
			tmp.setTenor(mcptProductMappedDto.getTenor());
			tmp.setGrate(mcptProductMappedDto.getGrate());
			tmp.setGrateName(mcptProductMappedDto.getGrateName());
			tmp.setProductCode(product.get("productCode"));
			tmp.setProductName(product.get("productName"));
			ProductMappedList.add(tmp);
		}
		//根据产品小类查询是否已经存在
		List<Integer> productCodeList = null;
		if(ProductMappedList!=null&&ProductMappedList.size()>0){
			productCodeList = productTypeDao.selectProductMappedChannelByProductCodes(ProductMappedList);
		}
		//根据查询到的产品小类主键id,更改delete_flag为1删除状态
		if(productCodeList!=null&&productCodeList.size()>0){
			productTypeDao.updateProductMappedChannelByProductCodes(productCodeList);
		}
		productTypeDao.insertproductMappedChannel(ProductMappedList);
	}

	@Override
	public List<McptProductMappedDto> searchProductMapped(McptProductMappedDto mcptProductMappedDto)
			throws MarvelsBusinessException {
		if (mcptProductMappedDto != null) {
			if (mcptProductMappedDto.getCurrentPage() != null && mcptProductMappedDto.getPageSize() != null) {
				PageHelper.startPage(mcptProductMappedDto.getCurrentPage(), mcptProductMappedDto.getPageSize());
			}
		}
		return productTypeDao.selectProductMapped(mcptProductMappedDto);
	}

	@Override
	public void modifyProductMappedChannel(McptProductMappedDto mcptProductMappedDto) throws MarvelsBusinessException {
		if (mcptProductMappedDto == null) {
			throw new MarvelsBusinessException("产品映射渠道参数不能为空");
		}
		mcptProductMappedDto.setUpdatedBy(CacheUtils.getCacheUser().getUserName());
		productTypeDao.updateProductMappedChannel(mcptProductMappedDto);
	}

	@Override
	public List<McptProductFundRelationDto> getRateByProductCode(McptProduct dto) throws MarvelsBusinessException {
		if (dto == null) {
			throw new MarvelsBusinessException("查询参数为空");
		}
		return productTypeDao.selectRateByProductCode(dto);
	}
	@Override
	public Map<String, Object> getAllSelector() throws MarvelsBusinessException{
		Map<String,Object> result = new HashMap<String,Object>();
		//还款计算方式
		List<McptAmortMethodDTO> amortMethodList = mcptAmortMethodMapper.getListByParam(result);
		//还款顺序
		result.put("status", "0");
		List<McptPaymentSequenceDTO> paymentSequence = mcptPaymentSequenceMapper.getListByParam(result);
		//客户等级
		List<McptCustomerTypeParamDTO> customerTypes = McptCustomerTypeParamDao.selectAllType();

		result.put("amortMethodList", amortMethodList);
		result.put("paymentSequence", paymentSequence);
		result.put("customerTypes", customerTypes);

		return result;
	}
	@Override
	public Map<String, Object> findProductMappedSelector(McptProductMappedDto mcptProductMappedDto)throws MarvelsBusinessException {
		Map<String,Object> result = new HashMap<String,Object>();
		McptProductTypeDto productType = new McptProductTypeDto();
		//获取版本号
		List<McptProductTypeDto>  versions = productTypeDao.selectProductVersion(productType);
		//获取产品大类
		List<McptProductTypeDto> products = productTypeDao.selectProduct(productType);
		//获取客户评级
		List<McptCustomerTypeParamDTO> customerTypes = McptCustomerTypeParamDao.selectAllType();

		result.put("versions", versions);
		result.put("products", products);
		result.put("customerTypes", customerTypes);
		return result;
	}
	@Override
	public List<OrganizationInfoDTO> findOrgInfoList(OrganizationInfoDTO orgDto) throws MarvelsBusinessException {
		return umConsumerApi.findOrgInfoByOrgCode(orgDto);
	}

	@Override
	public McptProductTypeDto getProduct(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<McptProductTypeChannelDto> selectByChannelCode(McptProductTypeChannelDto dto) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return productTypeDao.selectByChannelCode(dto);
	}

	@Override
	public List<McptProductTypeDto> productOffLine(ProductRequestDTO reqParam) throws MarvelsBusinessException {
		// 查询云展业产品
		List<McptProductTypeDto> ptypes = productTypeDao.selectProductTypeByPatternType("B");
		String yzyProductList=ServerProperties.getValue(configPropertiesPath, "resource.properties", "yzy.product.list");
		yzyProductList = yzyProductList==null?"":yzyProductList;
		String[] yzyList =  yzyProductList.split(",");
		List yzyListDatas = java.util.Arrays.asList(yzyList);
		List<McptProductTypeDto> results = new ArrayList();
		for(McptProductTypeDto ptype:ptypes) {
			if(yzyListDatas.contains(ptype.getProductType())) {
				results.add(ptype);
			}
		}
//		yzy.product.list
		return results;
	}

//	@Override
//	public List<McptProductTypeDto> productOnLine(ProductRequestDTO reqParam) throws MarvelsBusinessException {
//		//查询云科贷产品
//		List<McptProductTypeDto> ptypes = productTypeDao.selectProductTypeByPatternType("A");
//		//查询是否是内测账号
//		String ykdProductListKey = "ykd.product.list";//ykd.product.list.nei.ce
//		if(reqParam.getUserId() != null) {
//			String ncCustListKey = "nc_cust_list";// NC_CUST
//			String ncCustListStr=ServerProperties.getValue(configPropertiesPath, "resource.properties",ncCustListKey);
//			String[] ncCusts =  ncCustListStr.split(",");
//			List ncCustLists = java.util.Arrays.asList(ncCusts);
//			if(ncCustLists.contains(reqParam.getUserId().toString())) {
//				ykdProductListKey = "ykd.product.list.nei.ce";
//			}
//		}
//		//配置显示产品
//		String ykdProductListStr=ServerProperties.getValue(configPropertiesPath, "resource.properties",ykdProductListKey);
//		String[] ykdProducts =  ykdProductListStr.split(",");
//		List ykdProductList = java.util.Arrays.asList(ykdProducts);
//		//配置跟产品大类取交集
//		List<McptProductTypeDto> result = new ArrayList();
//		for(McptProductTypeDto ptype : ptypes) {
//			if(ykdProductList.contains(ptype.getProductType())) {
//				result.add(ptype);
//			}
//		}
//		return result;
//	}

	@Override
	public McptProductTypeDto queryOnlineProduct(McptProductTypeDto mcptProductTypeDto) throws MarvelsBusinessException {
		if (mcptProductTypeDto != null) {
			if (mcptProductTypeDto.getCurrentPage() != null && mcptProductTypeDto.getPageSize() != null) {
				PageHelper.startPage(mcptProductTypeDto.getCurrentPage(), mcptProductTypeDto.getPageSize());
			}
		}
		return productTypeDao.queryOnlineProduct(mcptProductTypeDto);
	}

	@Override
	public Map<String, Object> getHomeInfo() throws MarvelsBusinessException {
		// 查询产品、期数信息
		Map<String, Object> rtnMap = productTypeDao.getHomeInfo();

		// 查询服务费率
		Map<String, Object> feeParam = new HashMap<>();
		List<McptConsultingFeeDTO> feeList = mcptConsultingFeeMapper.getListByParam(feeParam);
		if (feeList != null && feeList.size() > 0) {
			McptConsultingFeeDTO feeDto = feeList.get(0);
			rtnMap.put("consultingFee", feeDto.getPercentage());
		}
		McptProductTypeDto dto = new McptProductTypeDto();
		dto.setProductType((String)rtnMap.get("productType"));
		List<McptProductTypeTermDto> producntTypeTerm = productTypeDao.selectTermByProductCode(dto);
		//添加支持的期数
		List<Integer> terms = new ArrayList<Integer>();
		if(CollectionUtils.isNotEmpty(producntTypeTerm)) {
			terms = producntTypeTerm.stream().map(McptProductTypeTermDto :: getTerm).collect(Collectors.toList());
		}
		rtnMap.put("terms", terms);
		return rtnMap;
	}

	/**
	 * 查询产品咨询费率
	 * @author yangjy
	 */
	@Override
	public Map<String, Object> queryByConsultingFeeCode(McptProductDto dto) {
		McptProduct mcptProduct = mcptProductMapper.queryByProductType((String) dto.getProductType());
		String consultingFeeCode = "";
		if(null != mcptProduct){
			consultingFeeCode = mcptProduct.getConsultingFeeCode();
		}
		Map<String, Object> respMap = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(consultingFeeCode)) {
			respMap = mcptProductMapper.queryByConsultingFeeCode(consultingFeeCode);
		}
		respMap.put("manageFeePercentage", mcptProduct.getManageFeePercentage());
		respMap.put("cycle", mcptProduct.getCycle());
		return respMap;
	}

	/**
	 * 查询产品咨询费率
	 * @author yangjy
	 */
	@Override
	public McptProduct queryProductByProductType(String productType) {
		return mcptProductMapper.queryByProductType(productType);
	}

}
